package com.javalong.affairs.utils;

import java.util.LinkedList;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class AppUtils {
	private static AppUtils appUtils = null;
	private LinkedList<Activity> activitys = null;

	private AppUtils() {
		activitys = new LinkedList<Activity>();
	}

	public static AppUtils getInstance() {
		if (appUtils != null) {
			return appUtils;
		} else {
			appUtils = new AppUtils();
			return appUtils;
		}
	}

	// add Activity
	public void addActivity(Activity activity) {
		activitys.add(activity);
	}

	public void exit() {
		try {
			for (Activity activity : activitys) {
				if (activity != null)
					activity.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.exit(0);
		}
	}

	/**
	 * 
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static final boolean isApkInstalled(Context context,
			String packageName) {
		try {
			context.getPackageManager().getApplicationInfo(packageName,
					PackageManager.GET_UNINSTALLED_PACKAGES);
			return true;
		} catch (NameNotFoundException e) {
			return false;
		}
	}

}
